/****************************************************************************
 * 2025.7 GRAVEROBBER-PC
 ****************************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using QAssetBundle;
using UnityEngine;
using UnityEngine.UI;
using QFramework;
using UnityEngine.U2D;

namespace ProjectSurvival
{
	public partial class AchievementPanel : UIElement,IController
	{
		ResLoader _resLoader = ResLoader.Allocate();
		private SpriteAtlas _iconAtlas;
		private void Awake()
		{
			AchievementItemTemplate.Hide();
			_resLoader.Add2Load<SpriteAtlas>("Icon");
			_resLoader.LoadAsync(() =>
			{
				_iconAtlas = _resLoader.LoadSync<SpriteAtlas>("Icon");
			});
			//_iconAtlas = _resLoader.LoadSync<SpriteAtlas>("icon");
			
			//var _achievementSystme = this.GetSystem<AchievementSystem>();
			foreach (var item in this.GetSystem<AchievementSystem>().Items.OrderByDescending(i=>i.Unlocked))
			{
				AchievementItemTemplate.InstantiateWithParent(AchievementItemRoot)
					.Self(self =>
					{
						self.GetComponentInChildren<Text>().text = "<b>" + item.Name +
						                                 (item.Unlocked
							                                 ? "<color=green>【已完成】</color>"
							                                 : "" )+ "</b>\n" + item.Description;
						self.transform.Find("Icon").GetComponent<Image>().sprite = _iconAtlas.GetSprite(item.IconName);
					})
					.Show();
			}
			btnClose.onClick.AddListener(() =>
			{
				AudioKit.PlaySound(Buttonclick_wav.ButtonClick);
				this.Hide();
			});
		}

		protected override void OnBeforeDestroy()
		{
			_resLoader.Recycle2Cache();
			_resLoader = null;
		}

		public IArchitecture GetArchitecture()
		{
			return Global.Interface;
		}
	}
}